package ru.emms.codemeter.ui;

import com.intellij.notification.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import ru.emms.codemeter.DatabaseStateListener;
import ru.emms.codemeter.configuration.Settings;
import ru.emms.codemeter.ui.forms.ResultViewForm;

import javax.swing.event.HyperlinkEvent;

/**
 * User: crazycoder
 * Date: 02.03.11
 */
public class BaloonNotificationDatabaseListener
        implements DatabaseStateListener {

    private Logger log = Logger.getInstance(getClass().getName());

    private static final int STATE_SUCCESS = 1;
    private static final int STATE_ERROR = 2;
    private int databaseState = STATE_SUCCESS;
    private final Object mutex = new Object();
    private Notification notification;

    private ResultViewForm form;
    private Project project;

    public BaloonNotificationDatabaseListener(final Project project, final ResultViewForm form) {
        this.form = form;
        this.project = project;
    }

    @Override
    public void notifySuccess(String message) {
        if(databaseState != STATE_SUCCESS) {
            synchronized (mutex) {
                if(databaseState != STATE_SUCCESS) {
                    databaseState = STATE_SUCCESS;
                    // Убрать красные сообщения
                    if(!notification.isExpired()) {
                        notification.expire();
                    }
                    form.updateVersions();
                }
            }
        }
        form.showBottomMessage(message, "БД подключена");
    }

    @Override
    public void notifyError(String message) {
        if(databaseState != STATE_ERROR) {

            synchronized (mutex) {
                if(databaseState != STATE_ERROR) {
                    databaseState = STATE_ERROR;
                    form.updateVersions();
                    String errorText = "Произошла ошибка при работе с базой данных. <a href=\"\">Проверить настройки...</a>";
                    notification = new Notification("Codemeter: database working error", "Ошибка при работе с базой данных", errorText,
                            NotificationType.ERROR, new NotificationListener() {
                                public void hyperlinkUpdate(@NotNull final Notification notification, @NotNull HyperlinkEvent event) {
                                    try {
                                        Configurable component = ShowSettingsUtil.getInstance()
                                                .findProjectConfigurable(project, Settings.class);
                                        ShowSettingsUtil.getInstance().editConfigurable(project, component);
                                        notification.expire();
                                    } catch (Exception e) {
                                        log.error("Ошибка при отображении конфигурации", e);
                                    }
                                }
                            });
                    Notifications.Bus.notify(notification, NotificationDisplayType.STICKY_BALLOON, project);
                }
            }

        }
    }

}
